﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Users : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetParameters();

        if (!IsPostBack)
        {
            GetData();
        }
    }

    private void GetParameters()
    {
    }


    private void GetData()
    {
        //calling a "parametered" constructor of my own TableAdapter and passing it to the ObjectInstance property 
        gvUsers.DataSource= Dal.ExecuteDBsp("Admin_GetAllUsers");
        gvUsers.DataBind();
       
    }

    protected void btnDeleteUser_Click(Object sender, ImageClickEventArgs e)
    {
        Int32 iUserID;
        Int32.TryParse(((ImageButton)sender).CommandArgument.ToString(), out iUserID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iUserID);
        Dal.ExecuteDBsp("Admin_DeleteUser", Dal.arParams);
        GetData();

        if (Convert.ToInt32(Session["UserID"]) == iUserID)
        {
            Session["UserID"] = 0;
            Response.Redirect("Login.aspx");
        }
    }

    protected void btnAddUser_Click(Object sender, EventArgs e)
    {
        
        Dal.arParams.Clear();
        Dal.arParams.Add(txtUserName.Text.Trim());
        Dal.arParams.Add(txtPassword.Text.Trim());
        Dal.ExecuteDBsp("Admin_AddUser", Dal.arParams,true);
        GetData();
        txtUserName.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }


    protected void gvUsers_RowBound(object sender, GridViewRowEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Row.FindControl("btnDeleteNews").ClientID.Replace("_", "$");
            cb.OnClientCancel = "onCancel";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Row.Cells[1].Controls.Add(cb);
        }
        catch { }
    }
}
